// DlgEditLibrary.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditLibrary.h"
#include "DlgPickList.h"

// DlgEditLibrary dialog

IMPLEMENT_DYNAMIC(CDlgEditLibrary, CDialog)

CDlgEditLibrary::CDlgEditLibrary(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditLibrary::IDD, pParent)
	, m_szID(_T(""))
	, m_szDescription(_T(""))
	, m_szName(_T(""))
	, m_szEmail(_T(""))
	, m_szAddress(_T(""))
	, m_szPhone(_T(""))
	, m_szSubject(_T(""))
	, m_bProblemLibrary(FALSE)
	, m_bInternalILLLibrary(FALSE)
	, m_bExternalILLLibrary(FALSE)
	, m_szInformation(_T(""))
	, m_bDiverted(FALSE)
{
	m_bAddLibrary = FALSE;
	m_nID = -1;
	m_nOldID = -1;
	m_nIndexID = -1;
}

CDlgEditLibrary::~CDlgEditLibrary()
{
}

void CDlgEditLibrary::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_szDescription);
	DDX_Text(pDX, IDC_EDIT_LIBRARY_NAME, m_szName);
	DDX_Text(pDX, IDC_EDIT_LIBRARY_EMAIL, m_szEmail);
	DDX_Text(pDX, IDC_EDIT_ADDRESS, m_szAddress);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_szPhone);
	DDX_Text(pDX, IDC_EDIT_EMAIL_SUBJECT, m_szSubject);
	DDX_Control(pDX, IDC_SPIN_RECORD, m_ctrlRecord);	
	DDX_Check(pDX, IDC_CHECK_USER_LISTED, m_bUserListed);
	DDX_Check(pDX, IDC_CHECK_PROBLEM_LIBRARY, m_bProblemLibrary);
	DDX_Check(pDX, IDC_CHECK_INTERNAL_ILL_LIBRARY, m_bInternalILLLibrary);
	DDX_Check(pDX, IDC_CHECK_EXTERNAL_ILL_LIBRARY, m_bExternalILLLibrary);
	DDX_Text(pDX, IDC_EDIT_INFORMATION, m_szInformation);
	DDX_Check(pDX, IDC_CHECK_DIVERTED, m_bDiverted);
}


BEGIN_MESSAGE_MAP(CDlgEditLibrary, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditLibrary::OnBnClickedButtonDelete)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_RECORD, &CDlgEditLibrary::OnDeltaposSpinRecord)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditLibrary::OnBnClickedButtonApply)
END_MESSAGE_MAP()


// DlgEditLibrary message handlers

BOOL CDlgEditLibrary::OnInitDialog()
{
	CDialog::OnInitDialog();

	CStringArray szaLibrary;
	LIBRARY_LOOKUP Library;

	appConfig.m_SQL.GetDescriptions( CMySQLEx::library, &szaLibrary, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );

	// check if adding
	if( m_bAddLibrary )
	{
		return TRUE;
	}
	else
	{
		GetDlgItem( IDC_EDIT_ID )->EnableWindow( FALSE );
	}

	// pick list
	if( m_nID < 0 )
	{
		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaLibrary;
		if( PickListDlg.DoModal() == IDOK )
			m_nID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	// get library
	if( !appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nID ))
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Library.szDescription;
	m_szName = Library.szName;
	m_szEmail = Library.szEmail;
	m_szAddress = Library.szAddress;
	m_szPhone = Library.szPhone;
	m_szSubject = Library.szSubject;
	m_bUserListed = Library.nUserListed;
	m_szInformation = Library.szInformation;
	m_bDiverted = Library.bDiverted;

	// set flags
	m_bProblemLibrary = Library.nType & ID_PROBLEM_LIBRARY ? TRUE : FALSE;
	m_bInternalILLLibrary = Library.nType & ID_INTERNAL_ILL_LIBRARY ? TRUE : FALSE;
	m_bExternalILLLibrary = Library.nType & ID_EXTERNAL_ILL_LIBRARY ? TRUE : FALSE;

	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// disable ILL
	if( appConfig.m_Options.bDisableILL )
	{
		GetDlgItem( IDC_CHECK_INTERNAL_ILL_LIBRARY )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_CHECK_EXTERNAL_ILL_LIBRARY )->ShowWindow( SW_HIDE );
	}

	// disable diverted for library 0
	if( m_nID == 0 )
		GetDlgItem( IDC_CHECK_DIVERTED )->EnableWindow( FALSE );
	else
		GetDlgItem( IDC_CHECK_DIVERTED )->EnableWindow();

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditLibrary::OnOK()
{
	// save old id
	m_nOldID = m_nID;
	// update record
	UpdateRecord();
	// close dialog
	CDialog::OnOK();
}

void CDlgEditLibrary::OnCancel()
{
	CDialog::OnCancel();
}

void CDlgEditLibrary::OnBnClickedButtonApply()
{
	// save old id
	m_nOldID = m_nID;
	// update record
	UpdateRecord();
}

void CDlgEditLibrary::OnBnClickedButtonDelete()
{
	// update data
	UpdateData();

	// get library id
	m_nID = _tstol( m_szID );

	// check for 0
	if( m_nID == 0 )
	{
		CString szMessage;
		AfxFormatString1( szMessage, IDS_ERROR_DELETE_ITEM_0, _T("Library") );
		AfxMessageBox( szMessage );
		return;
	}
	// check if sure
	if( AfxMessageBox( IDS_DELETE_LIBRARY, MB_YESNO ) == IDNO )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::library, m_nID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("library_id"), m_nID, 0 );

	// remove from array
	m_naID.RemoveAt( m_nIndexID );
	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// reset index
	m_nIndexID = 0;
	m_nID = m_naID.GetAt( m_nIndexID );

	// get library
	LIBRARY_LOOKUP Library;
	if( !appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nID ))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Library.szDescription;
	m_szName = Library.szName;
	m_szEmail = Library.szEmail;
	m_szAddress = Library.szAddress;
	m_szPhone = Library.szPhone;
	m_szSubject = Library.szSubject;
	m_bUserListed = Library.nUserListed;
	m_szInformation = Library.szInformation;
	m_bDiverted = Library.bDiverted;

	// set flags
	m_bProblemLibrary = Library.nType & ID_PROBLEM_LIBRARY ? TRUE : FALSE;
	m_bInternalILLLibrary = Library.nType & ID_INTERNAL_ILL_LIBRARY ? TRUE : FALSE;
	m_bExternalILLLibrary = Library.nType & ID_EXTERNAL_ILL_LIBRARY ? TRUE : FALSE;

	//update data
	UpdateData( FALSE );
	// reset old id
	m_nOldID = -1;
}

void CDlgEditLibrary::OnDeltaposSpinRecord(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	// increment index
	m_nIndexID += pNMUpDown->iDelta;
	if( m_nIndexID > m_naID.GetUpperBound() )
	{
		Beep( 750, 300 );
		m_nIndexID = m_naID.GetUpperBound();
		//m_nIndexID = 0;
		return;
	}
	if( m_nIndexID < 0 )
	{
		Beep( 750, 300 );
		m_nIndexID = 0;
		//m_nIndexID = (INT)m_naID.GetUpperBound();
		return;
	}

	// get library
	LIBRARY_LOOKUP Library;
	if( !appConfig.m_SQL.GetDescriptionLibrary( &Library, m_naID.GetAt( m_nIndexID )))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), Library.nID );
	m_szDescription = Library.szDescription;
	m_szName = Library.szName;
	m_szEmail = Library.szEmail;
	m_szAddress = Library.szAddress;
	m_szPhone = Library.szPhone;
	m_szSubject = Library.szSubject;
	m_bUserListed = Library.nUserListed;
	m_szInformation = Library.szInformation;
	m_bDiverted = Library.bDiverted;

	// set flags
	m_bProblemLibrary = Library.nType & ID_PROBLEM_LIBRARY ? TRUE : FALSE;
	m_bInternalILLLibrary = Library.nType & ID_INTERNAL_ILL_LIBRARY ? TRUE : FALSE;
	m_bExternalILLLibrary = Library.nType & ID_EXTERNAL_ILL_LIBRARY ? TRUE : FALSE;

	// set library ID
	m_nID = _tstol( m_szID );

	// disable diverted for library 0
	if( m_nID == 0 )
		GetDlgItem( IDC_CHECK_DIVERTED )->EnableWindow( FALSE );
	else
		GetDlgItem( IDC_CHECK_DIVERTED )->EnableWindow();

	//update data
	UpdateData( FALSE );

	*pResult = 0;
}

BOOL CDlgEditLibrary::UpdateRecord()
{
	// update data
	UpdateData();

	// check data
	if( m_szID.IsEmpty() ||	m_szDescription.IsEmpty() || m_szName.IsEmpty() || m_szEmail.IsEmpty() || m_szAddress.IsEmpty() || m_szSubject.IsEmpty() )
	{
		AfxMessageBox( _T("Data missing.\n\nPlease complete all data fields.") );
		return FALSE;
	}

	// get library id
	m_nID = _tstol( m_szID );

	// check for overwrite
	if( m_bAddLibrary )
	{
		if( appConfig.m_SQL.GetRowCount( CMySQLEx::library, m_nID ) > 0 )
		{
			if( AfxMessageBox( IDS_CHANGE_LIBRARY, MB_YESNO ) == IDNO )
			{
				// return
				return FALSE;
			}
		}
	}

	// check for update
	if( m_nOldID != -1 && m_nOldID != m_nID )
	{
		if( AfxMessageBox( IDS_UPDATE_LIBRARY, MB_YESNO ) == IDYES )
		{
			// update requests
			appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("library_id"), m_nOldID, m_nID );
		}		
	}

	CStringArray szaLibrary;
	LIBRARY_LOOKUP Library;
	Library.nUserListed = 0;

	// set variables
	Library.nID = m_nID;
	Library.szDescription = m_szDescription;
	Library.szName = m_szName;
	Library.szEmail = m_szEmail;
	Library.szAddress = m_szAddress;
	Library.szPhone = m_szPhone;
	Library.szSubject = m_szSubject;
	Library.nUserListed = m_bUserListed;
	Library.szInformation = m_szInformation;
	Library.bDiverted = m_bDiverted;

	INT nType = 0;
	if( m_bProblemLibrary )
		nType |= ID_PROBLEM_LIBRARY;
	if( m_bInternalILLLibrary )
		nType |= ID_INTERNAL_ILL_LIBRARY;
	if( m_bExternalILLLibrary )
		nType |= ID_EXTERNAL_ILL_LIBRARY;
	Library.nType = nType;

	// save it
	appConfig.m_SQL.SetDescriptionLibrary( &Library, m_nID, m_bAddLibrary );

	// fetch descriptions
	appConfig.m_SQL.GetDescriptions( CMySQLEx::library, &szaLibrary, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );
	// reset old id
	m_nOldID = -1;

	// return true
	return TRUE;
}
